<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>vue2_base</title>
    <style>
      .box {
        width: 100px;
        height: 100px;
        background-color: yellowgreen;
      }
      .box2 {
        background-color: pink;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <h1>{{words}}</h1>
      <button @click="words+='~'">监听简单数据类型</button>
      <h1>{{obj.words}}</h1>
      <button @click="obj.words+='~'">监听复杂数据类型</button>
      <hr />
      <h1>{{list}}</h1>
      <button @click="addItem">添加一个与元素</button>
    </div>
    <script src="./vue.min.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        // data 声明变量
        data: {
          words: "苹果",
          obj: {
            words: "苹果",
            price: 18,
          },
          list: [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }],
        },
        // methods 声明方法
        methods: {
          addItem() {
            this.list.push({ id: 6 });
          },
        },
        // computed 计算属性
        computed: {},
        // watch 监听
        watch: {
          words(newValue, oldValue) {
            console.log(newValue, oldValue);
          },
          // 复杂数据类型监听
          obj: {
            handler(newValue, oldValue) {
              console.log(newValue, oldValue);
            },
            deep: true,
            immdiate: true,
          },
          // 监听对象的某个属性
          "obj.words"(newValue, oldValue) {
            console.log(newValue, oldValue);
          },
          // 监听数组发生变化，需要使用数组的7个方法：push、pop、shift、unshift、splice、sort、reverse
          list(newValue, oldValue) {
            console.log(newValue, oldValue);
          },
        },
        beforeCreate() {
          console.log("beforeCreate创建前");
        },
        created() {
          console.log("created创建后");
        },
        beforeMount() {
          console.log("beforeMount挂载前");
        },
        mounted() {
          console.log("mounted挂载后");
        },
        beforeUpdate() {
          console.log("beforeUpdate更新前");
        },
        updated() {
          console.log("updated更新后");
        },
        beforeDestroy() {
          console.log("beforeDestroy销毁前");
        },
        destroyed() {
          console.log("destroyed销毁后");
        },
      });
    </script>
  </body>
</html>
